<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class auction extends template_plugin
{
	var $ipsclass;
	
	var $name = "Auction";
	var $multiple = 0;
	
	var $settings = array();
	
	var $order = array(
							'item' => 'item',
							'icon' => '',
							'name' => '',
							'category' => '',
							'time' => 'time_end',
							'price' => 'highest_bid',
							'bid' => 'num_bids',
						);
	var $order_by = array(
							'ASC' => 1,
							'DESC' => -1,
						);
	var $order_default = array(
									'item' => 'ASC',
									'icon' => 'ASC',
									'name' => 'ASC',
									'category' => 'ASC',
									'time' => 'ASC',
									'price' => 'ASC',
									'bid' => 'ASC',
								);
	
	function _load(&$plugin)
	{
		parent::_load($plugin);
		
		if($plugin['link'])
		{
			$this->settings = unserialize($plugin['plugin_config']);
		}
		else
		{
			$this->settings['add_item'] = 1;
			$this->settings['min_list_day'] = 3;
			$this->settings['max_list_day'] = 10;
			$this->settings['percent_resell'] = 75;
			$this->settings['subject'] = "{bidder_lower}, you have been out bidded";
			$this->settings['out_bid_msg'] = 
<<<EOF
{bidder_lower},
You have been outbidded on this item, {item_name}. The current price is {price} by {bidder_higher}.

[b]This is an automated message[/b]
EOF;
		}
	}
	
	function acp_menu()
	{
		return array(
					  1 => array('Current Listing','code=plugin&do=run&link='.$this->plugin['link'].'&action=list'),
					  2 => array('Completed Listing','code=plugin&do=run&link='.$this->plugin['link'].'&action=complete'),
					);
	}
	
	function acp()
	{
		$redirect = array(
							'list' => 'list_auctions',
							'complete' => 'list_completed',
							'bids' => 'list_bids',
						);
		
		if(!isset($redirect[$this->ipsclass->input['action']]))
		{
			$this->ipsclass->input['action'] = 'list';
		}
		
		$function = 'acp_'.$redirect[$this->ipsclass->input['action']];
		$this->$function();
	}
	
	function acp_list_auctions()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 50;
		
		$this->ipsclass->admin->page_title  = "Welcome to auction list for Auction";
		$this->ipsclass->admin->page_detail = "Welcome to auction list for Auction";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'List Auctions' );
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_auction_item'));
		$page = $this->ipsclass->adskin->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'USE_ST'       => 'st',
																	'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																	'L_SINGLE'     => "Single Page",
																	'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=list"
															)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"             , "1%"  );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "IP Address"       , ""    );
		$this->ipsclass->adskin->td_header[] = array( "Seller"           , ""    );
		$this->ipsclass->adskin->td_header[] = array( "Bids/Time Ending" , ""    );
		$this->ipsclass->adskin->td_header[] = array( "Bidder/Price"     , ""    );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Auction Listing");
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'i.*',
												'from' => array('market_auction_item'=>'i'),
												'add_join' => array(
																		array(
																				'select' => 'COUNT(b.b_id) as num_bids',
																				'from' => array('market_auction_bids'=>'b'),
																				'where' => 'i.a_id=b.aitem_id',
																				'type' => 'left'
																			),
																		array(
																				'select' => 's.members_display_name as seller_name, s.mgroup as seller_group',
																				'from' => array('members'=>'s'),
																				'where' => 's.id=i.member_id',
																				'type' => 'left'
																			),
																		array(
																				'select' => 'm.members_display_name as bidder_name, m.mgroup as bidder_group',
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=i.highest_id',
																				'type' => 'left'
																			),
																	),
												'where' => "time_end > ".time(),
												'order' => 'time_end ASC',
												'group' => 'b.aitem_id',
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
				$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
				continue;
			}
			
			$item['num_bid'] = $item['num_bids'];
			$item['num_bids'] = $this->ipsclass->do_number_format($item['num_bids']).($item['num_bids'] == 1 ? " Bid" : " Bids");
			$item['highest_bid'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<a href='".$this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?autocom=market&amp;plugin=".$this->ipsclass->input['link']."&amp;do=item&amp;item=".$item['a_id']."'><b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b></a><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																				  $item['ip_address'],
																				  $this->ipsclass->market_admin->format_name($item['member_id'],$item['seller_name'],$item['seller_group']),
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;item=".$item['a_id']."&amp;action=bids'>".$item['num_bids']."</a><br />".$this->ipsclass->get_date($item['time_end'],'LONG'),
																				  ($item['num_bid'] == 0 ? "---" : $this->ipsclass->market_admin->format_name($item['highest_id'],$item['bidder_name'],$item['bidder_group']))."<br />".$item['highest_bid'],
																		 )      );
		}
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No auction items",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_list_completed()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 50;
		
		$this->ipsclass->admin->page_title  = "Welcome to completed listing for Auction";
		$this->ipsclass->admin->page_detail = "Welcome to completed listing for Auction";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'Completed Auctions' );
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_auction_item'));
		$page = $this->ipsclass->adskin->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'USE_ST'       => 'st',
																	'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																	'L_SINGLE'     => "Single Page",
																	'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=list"
															)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"             , "1%"  );
		$this->ipsclass->adskin->td_header[] = array( "Name/Description" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "IP Address"       , ""    );
		$this->ipsclass->adskin->td_header[] = array( "Seller"           , ""    );
		$this->ipsclass->adskin->td_header[] = array( "Bids/Time Ending" , ""    );
		$this->ipsclass->adskin->td_header[] = array( "Winner/Price"     , ""    );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Completed Listing");
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'i.*',
												'from' => array('market_auction_item'=>'i'),
												'add_join' => array(
																		array(
																				'select' => 'COUNT(b.b_id) as num_bids',
																				'from' => array('market_auction_bids'=>'b'),
																				'where' => 'i.a_id=b.aitem_id',
																				'type' => 'left'
																			),
																		array(
																				'select' => 's.members_display_name as seller_name, s.mgroup as seller_group',
																				'from' => array('members'=>'s'),
																				'where' => 's.id=i.member_id',
																				'type' => 'left'
																			),
																		array(
																				'select' => 'm.members_display_name as bidder_name, m.mgroup as bidder_group',
																				'from' => array('members'=>'m'),
																				'where' => 'm.id=i.highest_id',
																				'type' => 'left'
																			),
																	),
												'where' => "time_end < ".time(),
												'order' => 'time_end DESC',
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				continue;
			}
			
			$item['num_bid'] = $item['num_bids'];
			$item['num_bids'] = $this->ipsclass->do_number_format($item['num_bids']).($item['num_bids'] == 1 ? " Bid" : " Bids");
			$item['highest_bid'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																				  "<a href='".$this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?autocom=market&amp;plugin=".$this->ipsclass->input['link']."&amp;do=item&amp;item=".$item['a_id']."'><b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b></a><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																				  $item['ip_address'],
																				  "<a href='".$this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?showuser=".$item['member_id']."&amp;tab=market' target='_blank'>".$this->ipsclass->make_name_formatted($item['seller_name'],$item['seller_group'])."</a>",
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;item=".$item['a_id']."&amp;action=bids'>".$item['num_bids']."</a><br />".$this->ipsclass->get_date($item['time_end'],'LONG'),
																				  $item['num_bid'] == 0 ? "No Winner" : "<a href='".$this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?showuser=".$item['highest_id']."&amp;tab=market' target='_blank'>".$this->ipsclass->make_name_formatted($item['bidder_name'],$item['bidder_group'])."</a><br />".$item['highest_bid'],
																		 )      );
		}
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No auction items",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_list_bids()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$this->ipsclass->admin->page_title  = "Welcome to bid history for Auction";
		$this->ipsclass->admin->page_detail = "Welcome to bid history for Auction";
		$this->ipsclass->admin->nav[] 		= array( '', 'Bid History' );
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->admin->error("Cannot find that item");
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
			$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
			$this->ipsclass->admin->redirect($this->ipsclass->form_code_js,"...");
		}
		
		$this->ipsclass->adskin->td_header[] = array( "Bidder"      , "" );
		$this->ipsclass->adskin->td_header[] = array( "Maximum Bid" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Bid Time"    , "" );
		$this->ipsclass->adskin->td_header[] = array( "IP Address"  , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Bid History");
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'b.*',
												'from' => array('market_auction_bids'=>'b'),
												'add_join' => array(
																		array(
																				'select' => 'm.members_display_name, m.mgroup',
																				'from' => array('members'=>'m'),
																				'where' => "m.id=b.member_id",
																				'type' => 'left',
																			),
																	),
												'where' => "b.aitem_id=".$this->ipsclass->input['item'],
												'order' => 'b.highest_bid DESC, b.bid_time ASC',
									)		);
		$this->ipsclass->DB->exec_query();
		while($bid = $this->ipsclass->DB->fetch_row())
		{
			if($this->ipsclass->numbers->compare($bid['highest_bid'],$item['highest_bid']) >= 0)
			{
				$bid['bid_amount'] = $this->ipsclass->do_number_format($item['highest_bid']);
			}
			else
			{
				$bid['bid_amount'] = $this->ipsclass->do_number_format($bid['highest_bid']);
			}
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																				  "<a href='".$this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?showuser=".$item['member_id']."&amp;tab=market' target='_blank'>".$this->ipsclass->make_name_formatted($bid['members_display_name'], $bid['mgroup'])."</a>",
																				  $this->ipsclass->do_number_format($bid['highest_bid']),
																				  $this->ipsclass->get_date($bid['bid_time'],'LONG'),
																				  $bid['ip_address'],
																		 )      );
		}
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No bids so far",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_settings()
	{
		$this->ipsclass->parser->parse_smilies   = 1;
		$this->ipsclass->parser->parse_html      = 1;
		$this->ipsclass->parser->parse_bbcode    = 1;
		
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$value = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->settings['out_bid_msg'] ) );
			$value = $this->ipsclass->parser->convert_ipb_html_to_html( $value );
		}
		else
		{
			$value = $this->ipsclass->parser->pre_edit_parse( $this->settings['out_bid_msg'] );
		}
		
		$html = "";
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow users to add item?</b>",
															$this->ipsclass->adskin->form_yes_no('add_item',$this->settings['add_item']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Minimum number of days for an item to be listed</b>",
															$this->ipsclass->adskin->form_simple_input('min_list_day',$this->settings['min_list_day']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Maximum number of days for an item to be listed</b>",
															$this->ipsclass->adskin->form_simple_input('max_list_day',$this->settings['max_list_day']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>What percent of the sold price that will be the resell price?</b><br />If the item is sold at 100 and this is set to 75%, the resell price is 75.",
															$this->ipsclass->adskin->form_simple_input('percent_resell',$this->settings['percent_resell'])."%",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Subject:</b>",
															$this->ipsclass->adskin->form_input('subject',$this->settings['subject']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Message:</b><br />Tags (Both subject and message):<br />{bidder_lower} = Member Name of Lower Bidder<br />{bidder_higher} = Member Name of Higher Bidder<br />{price} = Current Price<br />{currency_name} = Currency Name<br />{item_name} = Item name (with url)",
															$this->ipsclass->han_editor->show_editor( $value, 'out_bid_msg' ),
													)	);
		
		return $html;
	}
	
	function acp_save_settings(&$input)
	{
		$save = array();
		$save['add_item'] = $this->ipsclass->input['add_item'] & 1;
		$save['min_list_day'] = intval($this->ipsclass->input['min_list_day']);
		$save['min_list_day'] = $save['min_list_day'] <= 0 ? 1 : $save['min_list_day'];
		$save['max_list_day'] = intval($this->ipsclass->input['max_list_day']);
		$save['max_list_day'] = $save['max_list_day'] < $save['min_list_day'] ? $save['min_list_day'] : $save['max_list_day'];
		$save['percent_resell'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['percent_resell']);
		$save['subject'] = $this->ipsclass->input['subject'];
        $save['out_bid_msg'] = $this->ipsclass->han_editor->process_raw_post( 'out_bid_msg' );
		$save['out_bid_msg'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $save['out_bid_msg'] ) );
		
		$input['plugin_config'] = serialize($save);
	}
	
	function acp_install()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/sql/'.$this->ipsclass->vars['sql_driver'].'_plugin_queries.php','sql_plugin_queries');
		
		$this->ipsclass->DB->build_query_from_cache('table_auction_item',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_auction_bids',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
	}
	
	function acp_uninstall()
	{
		$this->ipsclass->DB->sql_drop_table( 'market_auction_item' );
		$this->ipsclass->DB->sql_drop_table( 'market_auction_bids' );
	}
	
	function run()
	{
		$this->ipsclass->load_template('skin_market_plugin');
		$this->ipsclass->load_language('lang_market_plugin');
		
		$this->ipsclass->market_class->title[] = array($this->plugin['name'],'market_'.$this->plugin['link']);
		if(in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['auction_list_items']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=sell'>".$this->ipsclass->lang['auction_sell_item']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=manage'>".$this->ipsclass->lang['auction_manage_items']."</a></b>",
														);
			
			$do_redirect = array(
									'list' => 'list_auctions',
									'item' => 'item_detail',
									'bid' => 'item_bid',
									'sell' => 'sell_item',
									'history' => 'bid_history',
									'edit' => 'edit_item_info',
									'save' => 'save_item_info',
									'manage' => 'manage_items',
									'pay' => 'paid_seller',
									'remove' => 'remove_item',
									'relist' => 'relist_item',
									'pay' => 'pay_for_item',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'list';
			}
		}
		else
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['auction_list_items']."</a></b>",
														);
			
			$do_redirect = array(
									'list' => 'list_auctions',
									'details' => 'item_detail',
									'history' => 'bid_history',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'list';
			}
		}
		
		$func = 'run_'.$do_redirect[$this->ipsclass->input['do']];
		$this->$func();
		
		$this->ipsclass->output = str_replace("\\\\\\n","\\n",$this->ipsclass->output);
	}
	
	function run_list_auctions()
	{
		$this->ipsclass->subtitle = $this->ipsclass->lang['auction_list_items'];
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['auction_list_items']."</a>");
		
		if(!isset($this->order[$this->ipsclass->input['order']]))
		{
			$this->ipsclass->input['order'] = 'time';
		}
		if(!isset($this->order_by[$this->ipsclass->input['by']]))
		{
			$this->ipsclass->input['by'] = $this->order_default[$this->ipsclass->input['order']];
		}
		
		if($this->ipsclass->input['by'] == $this->order_default[$this->ipsclass->input['order']])
		{
			if($this->order_default[$this->ipsclass->input['order']] == 'ASC')
			{
				$this->order_default[$this->ipsclass->input['order']] = 'DESC';
			}
			else
			{
				$this->order_default[$this->ipsclass->input['order']] = 'ASC';
			}
		}
		
		$where = "time_end > ".time();
		
		$items = array();
		$this->ipsclass->DB->build_query(array(
												'select' => 'i.*',
												'from' => array('market_auction_item'=>'i'),
												'add_join' => array(
																		array(
																				'select' => 'COUNT(b.b_id) as num_bids',
																				'from' => array('market_auction_bids'=>'b'),
																				'where' => 'i.a_id=b.aitem_id',
																				'type' => 'left',
																			),
																	),
												'where' => $where,
												'group' => 'i.a_id',
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
				$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
				continue;
			}
			
			$items[] = $item;
		}
		
		usort($items,array(&$this,'order'));
		foreach($items as $id => $item)
		{
			if($items[$id]['num_bids'] == 0)
			{
				$items[$id]['num_bids'] = "-";
			}
			else
			{
				$items[$id]['num_bids'] = $this->ipsclass->do_number_format($items[$id]['num_bids']);
			}
			$items[$id]['highest_bid'] = $this->ipsclass->numbers->format_currency($items[$id]['highest_bid']);
			$items[$id]['time_left'] = $this->ipsclass->numbers->time_left($items[$id]['time_end'],'TINY');
			
			$items[$id]['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$items[$id]['item']]->parse_single_description($items[$id])));
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->auction_list_items($items,$this->order_default);
	}
	
	function run_sell_item()
	{
		if(!$this->settings['add_item'])
		{
			$this->ipsclass->Error(array('MSG'=>'auction_no_sell_item'));
		}
		
		$this->ipsclass->input['inv_id'] = intval($this->ipsclass->input['inv_id']);
		$this->ipsclass->input['item_id'] = intval($this->ipsclass->input['item_id']);
		
		if($this->ipsclass->input['request_method'] == 'post' && is_array($this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]) && ($this->ipsclass->input['item_id'] == 0 || is_array($this->ipsclass->cache['market_items'][$this->ipsclass->input['item_id']])))
		{
			$this->ipsclass->input['start_bid'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['start_bid'],'int');
			if($this->ipsclass->numbers->compare($this->ipsclass->input['start_bid'],0) <= 0)
			{
				$this->ipsclass->Error(array('MSG'=>'auction_error_start_bid'));
			}
			
			$this->ipsclass->input['duration'] = intval($this->ipsclass->input['duration']);
			$this->ipsclass->input['duration'] = max($this->ipsclass->input['duration'],$this->settings['min_list_day'] * 86400);
			$this->ipsclass->input['duration'] = min($this->ipsclass->input['duration'],$this->settings['max_list_day'] * 86400);
			$this->ipsclass->input['duration'] = $this->ipsclass->input['duration'] - $this->ipsclass->input['duration'] % 86400;
			$this->ipsclass->input['user_description'] = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 'user_description' ) ) );
			
			$time = time();
			
			if($this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]['expired'] && $this->ipsclass->input['start_bid'] + $this->ipsclass->input['duration'] > $this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]['expired'])
			{
				$this->ipsclass->Error(array('MSG'=>'auction_no_end_after_expire'));
			}
			
			$item = array(
							'member_id' => $this->ipsclass->member['id'],
							'item' => $this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]['item'],
							'price_bought' => $this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]['price_bought'],
							'resell_price' => $this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]['resell_price'],
							'time_bought' => $this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]['time_bought'],
							'settings' => $this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]['settings'],
							'expired' => $this->ipsclass->market_class->inventory_id[$this->ipsclass->input['inv_id']]['expired'],
							'ip_address' => $this->ipsclass->ip_address,
							'time_start' => $time,
							'duration' => $this->ipsclass->input['duration'],
							'time_end' => $time + $this->ipsclass->input['duration'],
							'start_bid' => $this->ipsclass->input['start_bid'],
							'user_description' => $this->ipsclass->input['user_description'],
							'highest_bid' => $this->ipsclass->input['start_bid'],
							'highest_id' => 0,
						);
			
			$this->ipsclass->DB->do_insert('market_auction_item',$item);
			$item_id = $this->ipsclass->DB->get_insert_id();
			$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_inventory','where'=>"i_id=".$this->ipsclass->input['inv_id']));
			
			$this->ipsclass->market_class->redirect('auction_item_listed',"plugin=".$this->plugin['link']."&do=item&item=".$item_id);
		}
		else
		{
			$this->ipsclass->han_editor->remove_side_panel = 1;
	        
			$this->ipsclass->subtitle = $this->ipsclass->lang['auction_sell_item'];
			$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=sell'>".$this->ipsclass->lang['auction_sell_item']."</a>");
			
			$item = array();
			$item['js'] = "";
			$item['html'] = array();
			$item['dropdown'] = "";
			$item['dropdown'] .= "\t\t\t\t\t<option value='0'>-- ".$this->ipsclass->lang['list_all_items']." --</option>\n";
			foreach($this->ipsclass->market_class->inventory as $item_id => $item_group)
			{
				$item['js'] .= "inv_item[".$item_id."] = new Array();\n";
				$item['dropdown'] .= "\t\t\t\t\t<option value='".$item_id."'>".$this->ipsclass->cache['market_items'][$item_id]['item_name']."</option>\n";
				foreach($item_group as $inv_item)
				{
					$inv_item['expired'] = $inv_item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($inv_item['expired'],'TINY');
					$inv_item['time_bought'] = $this->ipsclass->get_date($inv_item['time_bought'],'TINY');
					$inv_item['fprice_bought'] = $inv_item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($inv_item['price_bought']);
					$inv_item['fresell_price'] = $inv_item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($inv_item['resell_price']);
					$inv_item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$inv_item['item']]->parse_single_description($inv_item)));
					$item['html'][] = $inv_item;
					$item['js'] .= "inv_item[".$item_id."][inv_item[".$item_id."].length] = all_item.length;\n";
					$item['js'] .= "all_item[all_item.length] = new Array(".$inv_item['i_id'].",'".str_replace("'","\'",$this->ipsclass->market_class->unhtmlentities($this->ipsclass->cache['market_items'][$inv_item['item']]['item_name']))."','".str_replace("'","\'",$inv_item['time_bought'])."',".$inv_item['price_bought'].");\n";
				}
			}
			
			$item['duration'] = "";
			for($i = intval($this->settings['min_list_day']); $i <= $this->settings['max_list_day']; $i++)
			{
				if($i == 1)
				{
					$item['duration'] .= "\t\t\t\t\t<option value='". 86400 ."'>".$this->ipsclass->lang['one_day']."</option>\n";
				}
				else
				{
					$item['duration'] .= "\t\t\t\t\t<option value='".($i*86400)."'>".sprintf($this->ipsclass->lang['x_days'],$i)."</option>\n";
				}
			}
			
			$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->auction_sell_form($item,$this->ipsclass->han_editor->show_editor( '', 'user_description' ));
		}
	}
	
	function run_item_detail()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_invalid_item'));
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
			$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']);
		}
		
		$this->ipsclass->subtitle = $this->ipsclass->cache['market_items'][$item['item']]['item_name'];
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=item&amp;item={$this->ipsclass->input['item']}'>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</a>");
		
		$item['time_left'] = $this->ipsclass->numbers->time_left($item['time_end']);
		$item['time_ended'] = $this->ipsclass->get_date($item['time_end'],'SHORT');
		
		$bids = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_auction_bids','where'=>"aitem_id=".$this->ipsclass->input['item']));
		$item['bid_count'] = $bids['total'];
		$item['num_bids'] = $bids['total'] == 1 ? $this->ipsclass->lang['auction_one_bid'] : sprintf($this->ipsclass->lang['auction_x_bids'],$this->ipsclass->do_number_format($bids['total']));
		if($bids['total'] == 0)
		{
			$item['minimum_bid'] = $item['highest_bid'];
		}
		else
		{
			$item['minimum_bid'] = $this->ipsclass->numbers->add($item['highest_bid'],$this->min_increment($item['highest_bid']));
		}
		$item['minimum_bidding'] = $this->ipsclass->numbers->format_currency($item['minimum_bid']);
		$item['bid_amount'] = $item['highest_bid'];
		$item['highest_bid'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
		
		if($item['highest_id'])
		{
			$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$item['highest_id']));
			$item['highest_bidder'] = $this->ipsclass->market_class->format_name($member['id'],$member['members_display_name'],$member['mgroup']);
			
			if($item['highest_id'] == $this->ipsclass->member['id'])
			{
				$highest = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_bids','where'=>"aitem_id=".$this->ipsclass->input['item'],'order'=>'highest_bid DESC','limit'=>array(0,1)));
				$highest['max_bid'] = $this->ipsclass->numbers->format_currency($highest['highest_bid']);
				$item['high_bid'] = $highest;
			}
		}
		
		$seller = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$item['member_id']));
		$item['seller'] = $this->ipsclass->market_class->format_name($item['member_id'],$seller['members_display_name'],$seller['mgroup']);
		
		$item['start_bid'] = $this->ipsclass->numbers->format_currency($item['start_bid']);
		$item['time_start'] = $this->ipsclass->get_date($item['time_start'],'LONG');
		$item['duration'] = $item['duration'] == 86400 ? $this->ipsclass->lang['one_day'] : sprintf($this->ipsclass->lang['x_days'],$item['duration']/86400);
		
		$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
		
		$item['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->auction_item_detail($item);
	}
	
	function run_item_bid()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		$this->ipsclass->input['bid_amount'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['bid_amount'],'int');
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_invalid_item'));
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
			$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']);
		}
		if($item['time_end'] <= time())
		{
			$this->ipsclass->Error(array('MSG'=>'auction_has_ended'));
		}
		if($item['member_id'] == $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'auction_bid_self'));
		}
		
		$bids = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_auction_bids','where'=>"aitem_id=".$this->ipsclass->input['item']));
		
		if($bids['total'] == 0)
		{
			$minimum = $item['highest_bid'];
		}
		else
		{
			$minimum = $this->ipsclass->numbers->add($item['highest_bid'],$this->min_increment($item['highest_bid']));
		}
		
		$highest = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_bids','where'=>"aitem_id=".$this->ipsclass->input['item'],'order'=>'highest_bid DESC','limit'=>array(1)));
		if($highest['member_id'] == $this->ipsclass->member['id'])
		{
			// Update Bid
			$minimum = $item['highest_bid'];
		}
		
		if($this->ipsclass->numbers->compare($this->ipsclass->input['bid_amount'],$minimum) < 0)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_error_min_bid'));
		}
		
		if($highest['member_id'] == $this->ipsclass->member['id'])
		{
			$this->ipsclass->DB->do_update('market_auction_bids',array(
																		'highest_bid' => $this->ipsclass->input['bid_amount'],
																		'ip_address' => $this->ipsclass->ip_address,
										),"b_id=".$highest['b_id']);
			
			if($bids['total'] > 1)
			{
				$high_bid = $this->ipsclass->numbers->min($minimum,$this->ipsclass->input['bid_amount']);
				$this->ipsclass->DB->do_update('market_auction_item',array(
																				'highest_bid' => $high_bid,
									),"a_id=".$this->ipsclass->input['item']);
			}
			
			$this->ipsclass->market_class->redirect('auction_update_bid',"plugin=".$this->plugin['link']."&do=item&item=".$this->ipsclass->input['item']);
		}
		
		$high_bid = $item['highest_bid'];
		$high_bidder = $item['highest_id'];
		$message = 'auction_bid_highest_redirect';
		if($bids['total'] == 0)
		{
			$high_bid = $item['start_bid'];
			$high_bidder = $this->ipsclass->member['id'];
		}
		else
		{
			if($this->ipsclass->numbers->compare($highest['highest_bid'],$this->ipsclass->input['bid_amount']) >= 0)
			{
				$high_bid = $this->ipsclass->numbers->add($this->ipsclass->input['bid_amount'],$this->min_increment($this->ipsclass->input['bid_amount']));
				$high_bid = $this->ipsclass->numbers->min($high_bid,$highest['highest_bid']);
				$high_bidder = $highest['member_id'];
				$message = 'auction_bid_not_highest_redirect';
			}
			else
			{
				$high_bid = $this->ipsclass->numbers->add($highest['highest_bid'],$this->min_increment($highest['highest_bid']));
				$high_bid = $this->ipsclass->numbers->min($high_bid,$this->ipsclass->input['bid_amount']);
				$high_bidder = $this->ipsclass->member['id'];
			}
		}
		
		$this->ipsclass->DB->do_update('market_auction_item',array(
																		'highest_bid' => $high_bid,
																		'highest_id' => $high_bidder,
							),"a_id=".$this->ipsclass->input['item']);
		$this->ipsclass->DB->do_insert('market_auction_bids',array(
																	'aitem_id' => $this->ipsclass->input['item'],
																	'member_id' => $this->ipsclass->member['id'],
																	'highest_bid' => $this->ipsclass->input['bid_amount'],
																	'bid_time' => time(),
																	'ip_address' => $this->ipsclass->ip_address,
									)							);
		
		if($bids['total'] > 0)
		{
			require_once( ROOT_PATH.'sources/lib/func_msg.php' );
	 		
			$msg_lib           =  new func_msg();
			$msg_lib->ipsclass =& $this->ipsclass;
			$msg_lib->init();
			
			$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$highest['member_id']));
			if($high_bidder == $this->ipsclass->member['id'])
			{
				$this->settings['out_bid_msg'] = str_replace("{bidder_lower}",$member['members_display_name'],$this->settings['out_bid_msg']);
				$this->settings['out_bid_msg'] = str_replace("{bidder_higher}",$this->ipsclass->member['members_display_name'],$this->settings['out_bid_msg']);
				$this->settings['subject'] = str_replace("{bidder_lower}",$member['members_display_name'],$this->settings['subject']);
				$this->settings['subject'] = str_replace("{bidder_higher}",$this->ipsclass->member['members_display_name'],$this->settings['subject']);
				$msg_lib->to_by_id    = $highest['member_id'];
			}
			else
			{
				$this->settings['out_bid_msg'] = str_replace("{bidder_lower}",$this->ipsclass->member['members_display_name'],$this->settings['out_bid_msg']);
				$this->settings['out_bid_msg'] = str_replace("{bidder_higher}",$member['members_display_name'],$this->settings['out_bid_msg']);
				$this->settings['subject'] = str_replace("{bidder_lower}",$this->ipsclass->member['members_display_name'],$this->settings['subject']);
				$this->settings['subject'] = str_replace("{bidder_higher}",$member['members_display_name'],$this->settings['subject']);
				$msg_lib->to_by_id    = $this->ipsclass->member['id'];
			}
			
			$this->settings['out_bid_msg'] = str_replace("{price}",$this->ipsclass->numbers->format_currency($high_bid),$this->settings['out_bid_msg']);
			$this->settings['out_bid_msg'] = str_replace("{currency_name}",$this->ipsclass->vars['market_currency'],$this->settings['out_bid_msg']);
			$this->settings['out_bid_msg'] = str_replace("{item_name}","<a href='".$this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=item&amp;item=".$item['a_id']."'><b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b></a>",$this->settings['out_bid_msg']);
			$this->settings['subject'] = str_replace("{price}",$this->ipsclass->numbers->format_currency($high_bid),$this->settings['subject']);
			$this->settings['subject'] = str_replace("{currency_name}",$this->ipsclass->vars['market_currency'],$this->settings['subject']);
			$this->settings['subject'] = str_replace("{item_name}","<a href='".$this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=item&amp;item=".$item['a_id']."'><b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b></a>",$this->settings['subject']);
			
			$msg_lib->from_member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$item['member_id']));
			$msg_lib->msg_title   = $this->settings['subject'];
			$msg_lib->msg_post    = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->settings['out_bid_msg'] ) );
			$msg_lib->force_pm    = 1;
			
			$msg_lib->send_pm();
		}
		
		$this->ipsclass->market_class->redirect($message,"plugin=".$this->plugin['link']."&do=item&item=".$this->ipsclass->input['item']);
	}
	
	function run_bid_history()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_invalid_item'));
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
			$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']);
		}
		
		$this->ipsclass->subtitle = $this->ipsclass->lang['auction_bid_history'];
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=item&amp;item={$this->ipsclass->input['item']}'>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=history&amp;item={$this->ipsclass->input['item']}'>".$this->ipsclass->lang['auction_bid_history']."</a>");
		
		$item['time_left'] = $this->ipsclass->numbers->time_left($item['time_end']);
		
		$bids = array();
		$this->ipsclass->DB->build_query(array(
												'select' => 'b.*',
												'from' => array('market_auction_bids'=>'b'),
												'add_join' => array(
																		array(
																				'select' => 'm.members_display_name, m.mgroup',
																				'from' => array('members'=>'m'),
																				'where' => "m.id=b.member_id",
																				'type' => 'left',
																			),
																	),
												'where' => "b.aitem_id=".$this->ipsclass->input['item'],
												'order' => 'b.highest_bid DESC, b.bid_time ASC',
									)		);
		$this->ipsclass->DB->exec_query();
		while($bid = $this->ipsclass->DB->fetch_row())
		{
			$bid['members_display_name'] = $this->ipsclass->market_class->format_name($bid['member_id'],$bid['members_display_name'], $bid['mgroup']);
			$bid['bid_time'] = $this->ipsclass->get_date($bid['bid_time'],'LONG');
			if($this->ipsclass->numbers->compare($bid['highest_bid'],$item['highest_bid']) >= 0)
			{
				$bid['bid_amount'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
			}
			else
			{
				$bid['bid_amount'] = $this->ipsclass->numbers->format_currency($bid['highest_bid']);
			}
			$bids[] = $bid;
		}
		
		$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->auction_bid_history($item,$bids);
	}
	
	function run_edit_item_info()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_invalid_item'));
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
			$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']);
		}
		if($item['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'auction_not_your_item'));
		}
		
		$this->ipsclass->subtitle = $this->ipsclass->lang['auction_edit'];
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=item&amp;item={$this->ipsclass->input['item']}'>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=edit&amp;item={$this->ipsclass->input['item']}'>".$this->ipsclass->lang['auction_edit']."</a>");
		
		$this->ipsclass->han_editor->remove_side_panel = 1;
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$item['user_description'] = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $item['user_description'] ) );
			$item['user_description'] = $this->ipsclass->parser->convert_ipb_html_to_html( $item['user_description'] );
		}
		else
		{
			$item['user_description'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $item['user_description'] ) );
		}
        
		$duration = "";
		for($i = intval(max($this->settings['min_list_day'],$item['duration']/86400)); $i <= $this->settings['max_list_day']; $i++)
		{
			if($i == 1)
			{
				$duration .= "\t\t\t\t\t<option value='". 86400 ."'>".$this->ipsclass->lang['one_day']."</option>\n";
			}
			else
			{
				$duration .= "\t\t\t\t\t<option value='".($i*86400)."'>".sprintf($this->ipsclass->lang['x_days'],$i)."</option>\n";
			}
		}
		
		$item['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
		$item['time_bought'] = $this->ipsclass->get_date($item['time_bought'],'TINY');
		$item['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
		$item['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
		
		$bids = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_auction_bids','where'=>"aitem_id=".$this->ipsclass->input['item']));
		$item['num_bids'] = $bids['total'];
		if($bids['total'] != 0)
		{
			$item['start_bid'] = $this->ipsclass->numbers->format_currency($item['start_bid']);
		}
		
		$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->auction_edit_description($item,$this->ipsclass->han_editor->show_editor( $item['user_description'], 'user_description' ),$duration);
	}
	
	function run_save_item_info()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_invalid_item'));
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
			$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']);
		}
		if($item['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'auction_not_your_item'));
		}
		
		$this->ipsclass->input['duration'] = intval($this->ipsclass->input['duration']);
		$this->ipsclass->input['duration'] = max($this->ipsclass->input['duration'],$item['duration']);
		$this->ipsclass->input['duration'] = min($this->ipsclass->input['duration'],$this->settings['max_list_day'] * 86400);
		$this->ipsclass->input['duration'] = $this->ipsclass->input['duration'] - $this->ipsclass->input['duration'] % 86400;
		$this->ipsclass->input['user_description'] = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post( 'user_description' ) ) );
		
		$item_save = array(
							'duration' => $this->ipsclass->input['duration'],
							'time_end' => $item['time_start'] + $this->ipsclass->input['duration'],
							'user_description' => $this->ipsclass->input['user_description'],
						);
		
		$bids = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_auction_bids','where'=>"aitem_id=".$this->ipsclass->input['item']));
		if($bids['total'] == 0)
		{
			$item_save['start_bid'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['start_bid']);
			$item_save['highest_bid'] = $item_save['start_bid'];
		}
		
		$this->ipsclass->DB->do_update('market_auction_item',$item_save,"a_id=".$item['a_id']);
		
		$this->ipsclass->market_class->redirect('auction_item_info_saved',"plugin=".$this->plugin['link']."&do=item&item=".$this->ipsclass->input['item']);
	}
	
	function run_manage_items()
	{
		$this->ipsclass->subtitle = $this->ipsclass->lang['auction_manage_items'];
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=manage'>".$this->ipsclass->lang['auction_manage_items']."</a>");
		
		if(!isset($this->order[$this->ipsclass->input['order']]))
		{
			$this->ipsclass->input['order'] = 'time';
		}
		if(!isset($this->order_by[$this->ipsclass->input['by']]))
		{
			$this->ipsclass->input['by'] = $this->order_default[$this->ipsclass->input['order']];
		}
		
		if($this->ipsclass->input['by'] == $this->order_default[$this->ipsclass->input['order']])
		{
			if($this->order_default[$this->ipsclass->input['order']] == 'ASC')
			{
				$this->order_default[$this->ipsclass->input['order']] = 'DESC';
			}
			else
			{
				$this->order_default[$this->ipsclass->input['order']] = 'ASC';
			}
		}
		
		$this->ipsclass->input['st_b'] = intval($this->ipsclass->input['st_b']);
		$this->ipsclass->input['st_w'] = intval($this->ipsclass->input['st_w']);
		$this->ipsclass->input['st_l'] = intval($this->ipsclass->input['st_l']);
		$this->ipsclass->input['st_s'] = intval($this->ipsclass->input['st_s']);
		$this->ipsclass->input['st_o'] = intval($this->ipsclass->input['st_o']);
		$LIMIT = 10;
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(DISTINCT aitem_id) as total','from'=>'market_auction_bids','where'=>"member_id=".$this->ipsclass->member['id']));
		$page['bidded'] = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'CUR_ST_VAL'   => $this->ipsclass->input['st_b'],
																	'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_w=".$this->ipsclass->input['st_w']."&amp;st_l=".$this->ipsclass->input['st_l']."&amp;st_s=".$this->ipsclass->input['st_s']."&amp;st_o=".$this->ipsclass->input['st_o'],0,'st_b'),
																	'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_w=".$this->ipsclass->input['st_w']."&amp;st_l=".$this->ipsclass->input['st_l']."&amp;st_s=".$this->ipsclass->input['st_s']."&amp;st_o=".$this->ipsclass->input['st_o'],
																	'USE_ST'       => 'st_b',
																	'leave_out'    => 5,
															)	);
		
		$items = array();
		$items['bidded'] = array();
		$this->ipsclass->DB->build_query(array(
												'select' => 'COUNT(*) as num_bids, MAX(b.highest_bid) as high_bid',
												'from' => array('market_auction_bids'=>'b'),
												'add_join' => array(
																		array(
																				'select' => 'a.*',
																				'from' => array('market_auction_item'=>'a'),
																				'where' => 'a.a_id=b.aitem_id',
																				'type' => 'left',
																			),
																	),
												'where' => "b.member_id=".$this->ipsclass->member['id']." AND a.time_end>".time(),
												'group' => 'b.aitem_id',
												'order' => 'a.time_end ASC',
												'limit' => array($this->ipsclass->input['st_b'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
				$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
				continue;
			}
			$item['num_bids'] = $this->ipsclass->do_number_format($item['num_bids']);
			$item['high_bid'] = $this->ipsclass->numbers->format_currency($item['high_bid']);
			$item['highest_bid'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
			$item['time_left'] = $this->ipsclass->numbers->time_left($item['time_end'],'TINY');
			$items['bidded'][] = $item;
		}
		
		$total = $this->ipsclass->DB->build_and_exec_query(array(
																	'select' => 'COUNT(DISTINCT b.aitem_id) as total',
																	'from' => array('market_auction_bids'=>'b'),
																	'add_join' => array(
																							array(
																									'from' => array('market_auction_item'=>'a'),
																									'where' => 'a.a_id=b.aitem_id',
																									'type' => 'inner',
																								),
																						),
																	'where' => "b.member_id=".$this->ipsclass->member['id']." AND a.highest_id=".$this->ipsclass->member['id']." AND a.time_end<=".time()
															)	);
		$page['won'] = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'CUR_ST_VAL'   => $this->ipsclass->input['st_w'],
																	'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_b=".$this->ipsclass->input['st_b']."&amp;st_l=".$this->ipsclass->input['st_l']."&amp;st_s=".$this->ipsclass->input['st_s']."&amp;st_o=".$this->ipsclass->input['st_o'],0,'st_b'),
																	'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_b=".$this->ipsclass->input['st_b']."&amp;st_l=".$this->ipsclass->input['st_l']."&amp;st_s=".$this->ipsclass->input['st_s']."&amp;st_o=".$this->ipsclass->input['st_o'],
																	'USE_ST'       => 'st_w',
																	'leave_out'    => 5,
															)	);
		
		$items['won'] = array();
		$this->ipsclass->DB->build_query(array(
												'select' => 'COUNT(*) as num_bids',
												'from' => array('market_auction_bids'=>'b'),
												'add_join' => array(
																		array(
																				'select' => 'a.*',
																				'from' => array('market_auction_item'=>'a'),
																				'where' => 'a.a_id=b.aitem_id',
																				'type' => 'left'
																			),
																		array(
																				'select' => 'm.id as seller_id, m.members_display_name as seller, m.mgroup as seller_group',
																				'from' => array('members'=>'m'),
																				'where' => 'a.member_id=m.id',
																				'type' => 'left'
																			),
																	),
												'where' => "b.member_id=".$this->ipsclass->member['id']." AND a.highest_id=".$this->ipsclass->member['id']." AND a.time_end<=".time(),
												'group' => 'b.aitem_id',
												'order' => 'a.time_end ASC',
												'limit' => array($this->ipsclass->input['st_w'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				continue;
			}
			$item['seller'] = $this->ipsclass->market_class->format_name($item['seller_id'],$item['seller'],$item['seller_group']);
			$item['num_bids'] = $this->ipsclass->do_number_format($item['num_bids']);
			$item['price'] = $item['highest_bid'];
			$item['highest_bid'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
			$item['time_end'] = $this->ipsclass->get_date($item['time_end'],'TINY');
			if($item['paid'])
			{
				$item['status'] = $this->ipsclass->lang['auction_paid_and_sent'];
			}
			elseif($item['removed'])
			{
				$item['status'] = $this->ipsclass->lang['auction_not_paid_and_removed'];
			}
			else
			{
				$item['status'] = $this->ipsclass->lang['auction_not_paid'];
			}
			$items['won'][] = $item;
		}
		
		$total = $this->ipsclass->DB->build_and_exec_query(array(
																	'select' => 'COUNT(DISTINCT b.aitem_id) as total',
																	'from' => array('market_auction_bids'=>'b'),
																	'add_join' => array(
																							array(
																									'from' => array('market_auction_item'=>'a'),
																									'where' => 'a.a_id=b.aitem_id',
																									'type' => 'inner',
																								),
																						),
																	'where' => "b.member_id=".$this->ipsclass->member['id']." AND a.highest_id!=".$this->ipsclass->member['id']." AND a.time_end<=".time()
															)	);
		$page['lost'] = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'CUR_ST_VAL'   => $this->ipsclass->input['st_l'],
																	'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_b=".$this->ipsclass->input['st_b']."&amp;st_w=".$this->ipsclass->input['st_w']."&amp;st_s=".$this->ipsclass->input['st_s']."&amp;st_o=".$this->ipsclass->input['st_o'],0,'st_b'),
																	'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_b=".$this->ipsclass->input['st_b']."&amp;st_w=".$this->ipsclass->input['st_w']."&amp;st_s=".$this->ipsclass->input['st_s']."&amp;st_o=".$this->ipsclass->input['st_o'],
																	'USE_ST'       => 'st_l',
																	'leave_out'    => 5,
															)	);
		
		$items['lost'] = array();
		$this->ipsclass->DB->build_query(array(
												'select' => 'COUNT(*) as num_bids, MAX(b.highest_bid) as high_bid',
												'from' => array('market_auction_bids'=>'b'),
												'add_join' => array(
																		array(
																				'select' => 'a.*',
																				'from' => array('market_auction_item'=>'a'),
																				'where' => 'a.a_id=b.aitem_id',
																				'type' => 'left'
																			),
																		array(
																				'select' => 'm.id as seller_id, m.members_display_name as seller, m.mgroup as seller_group',
																				'from' => array('members'=>'m'),
																				'where' => 'a.member_id=m.id',
																				'type' => 'left'
																			),
																	),
												'where' => "b.member_id=".$this->ipsclass->member['id']." AND a.highest_id!=".$this->ipsclass->member['id']." AND a.time_end<=".time(),
												'group' => 'b.aitem_id',
												'order' => 'a.time_end ASC',
												'limit' => array($this->ipsclass->input['st_w'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				continue;
			}
			$item['seller'] = $this->ipsclass->market_class->format_name($item['seller_id'],$item['seller'],$item['seller_group']);
			$item['num_bids'] = $this->ipsclass->do_number_format($item['num_bids']);
			$item['highest_bid'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
			$item['high_bid'] = $this->ipsclass->numbers->format_currency($item['high_bid']);
			$item['time_end'] = $this->ipsclass->get_date($item['time_end'],'TINY');
			$items['lost'][] = $item;
		}
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_auction_item','where'=>"member_id=".$this->ipsclass->member['id']." AND time_end>".time()));
		$page['sell'] = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'CUR_ST_VAL'   => $this->ipsclass->input['st_s'],
																	'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_b=".$this->ipsclass->input['st_b']."&amp;st_w=".$this->ipsclass->input['st_w']."&amp;st_l=".$this->ipsclass->input['st_l']."&amp;st_o=".$this->ipsclass->input['st_o'],0,'st_b'),
																	'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_b=".$this->ipsclass->input['st_b']."&amp;st_w=".$this->ipsclass->input['st_w']."&amp;st_l=".$this->ipsclass->input['st_l']."&amp;st_o=".$this->ipsclass->input['st_o'],
																	'USE_ST'       => 'st_s',
																	'leave_out'    => 5,
															)	);
		
		$items['sell'] = array();
		$this->ipsclass->DB->build_query(array(
												'select' => 'i.*',
												'from' => array('market_auction_item'=>'i'),
												'add_join' => array(
																		array(
																				'select' => 'm.members_display_name, m.mgroup',
																				'from' => array('members'=>'m'),
																				'where' => 'i.highest_id=m.id',
																				'type' => 'left',
																			),
																		array(
																				'select' => 'COUNT(b.b_id) as num_bids',
																				'from' => array('market_auction_bids'=>'b'),
																				'where' => 'i.a_id=b.aitem_id',
																				'type' => 'left',
																			),
																	),
												'where' => "i.member_id=".$this->ipsclass->member['id']." AND i.time_end>".time(),
												'group' => 'i.a_id',
												'order' => 'i.time_end ASC',
												'limit' => array($this->ipsclass->input['st_s'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
				$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
				continue;
			}
			if($item['num_bids'] == 0)
			{
				$item['members_display_name'] = "---";
			}
			else
			{
				$this->ipsclass->market_class->format_name($item['highest_id'],$item['members_display_name'],$item['mgroup']);
			}
			$item['num_bids'] = $this->ipsclass->do_number_format($item['num_bids']);
			$item['highest_bid'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
			$item['time_left'] = $this->ipsclass->numbers->time_left($item['time_end'],'TINY');
			$items['sell'][] = $item;
		}
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_auction_item','where'=>"member_id=".$this->ipsclass->member['id']." AND time_end<=".time()));
		$page['sold'] = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'CUR_ST_VAL'   => $this->ipsclass->input['st_o'],
																	'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_b=".$this->ipsclass->input['st_b']."&amp;st_w=".$this->ipsclass->input['st_w']."&amp;st_l=".$this->ipsclass->input['st_l']."&amp;st_s=".$this->ipsclass->input['st_s'],0,'st_b'),
																	'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']."&amp;do=manage&amp;st_b=".$this->ipsclass->input['st_b']."&amp;st_w=".$this->ipsclass->input['st_w']."&amp;st_l=".$this->ipsclass->input['st_l']."&amp;st_s=".$this->ipsclass->input['st_s'],
																	'USE_ST'       => 'st_o',
																	'leave_out'    => 5,
															)	);
		
		$items['sold'] = array();
		$this->ipsclass->DB->build_query(array(
												'select' => 'i.*',
												'from' => array('market_auction_item'=>'i'),
												'add_join' => array(
																		array(
																				'select' => 'm.members_display_name, m.mgroup',
																				'from' => array('members'=>'m'),
																				'where' => 'i.highest_id=m.id',
																				'type' => 'left',
																			),
																		array(
																				'select' => 'COUNT(b.b_id) as num_bids',
																				'from' => array('market_auction_bids'=>'b'),
																				'where' => 'i.a_id=b.aitem_id',
																				'type' => 'left',
																			),
																	),
												'where' => "i.member_id=".$this->ipsclass->member['id']." AND i.time_end<=".time(),
												'group' => 'i.a_id',
												'order' => 'i.time_end ASC',
												'limit' => array($this->ipsclass->input['st_s'],$LIMIT),
										)	);
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				continue;
			}
			if($item['num_bids'] == 0)
			{
				$item['members_display_name'] = "---";
				$item['highest_bid'] = 0;
				if($item['removed'] == 1)
				{
					$item['status'] = $this->ipsclass->lang['auction_move_to_inventory'];
				}
				elseif($item['removed'] == 2)
				{
					$item['status'] = $this->ipsclass->lang['auction_relisted'];
				}
				else
				{
					$item['status'] = $this->ipsclass->lang['auction_no_action'];
				}
			}
			else
			{
				$item['members_display_name'] = $this->ipsclass->market_class->format_name($item['highest_id'],$item['members_display_name'],$item['mgroup']);
				if($item['paid'])
				{
					$item['status'] = $this->ipsclass->lang['auction_paid_and_sent'];
				}
				elseif($item['removed'] == 1)
				{
					$item['status'] = $this->ipsclass->lang['auction_not_paid_and_removed'];
				}
				elseif($item['removed'] == 2)
				{
					$item['status'] = $this->ipsclass->lang['auction_not_paid_and_relisted'];
				}
				else
				{
					$item['status'] = $this->ipsclass->lang['auction_not_paid'];
				}
			}
			$item['num_bids'] = $this->ipsclass->do_number_format($item['num_bids']);
			$item['highest_bid'] = $this->ipsclass->numbers->format_currency($item['highest_bid']);
			$item['time_end'] = $this->ipsclass->get_date($item['time_end'],'TINY');
			$items['sold'][] = $item;
		}
		
		$show = array();
		$show['market_auction_bidding'] = $this->ipsclass->market_class->toggle_show('market_auction_bidding');
		$show['market_auction_won'] = $this->ipsclass->market_class->toggle_show('market_auction_won');
		$show['market_auction_lost'] = $this->ipsclass->market_class->toggle_show('market_auction_lost');
		$show['market_auction_sell'] = $this->ipsclass->market_class->toggle_show('market_auction_sell');
		$show['market_auction_sold'] = $this->ipsclass->market_class->toggle_show('market_auction_sold');
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->auction_manage_items($items,$page,$show);
	}
	
	function run_relist_item()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_invalid_item'));
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']);
		}
		if($item['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'auction_not_your_item'));
		}
		if($item['time_end'] > time())
		{
			$this->ipsclass->Error(array('MSG'=>'auction_not_ended'));
		}
		
		if($this->ipsclass->input['request_method'] == 'post')
		{
			$this->ipsclass->input['start_bid'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['start_bid'],'int');
			if($this->ipsclass->numbers->compare($this->ipsclass->input['start_bid'],0) <= 0)
			{
				$this->ipsclass->Error(array('MSG'=>'auction_error_start_bid'));
			}
			
			$this->ipsclass->input['duration'] = intval($this->ipsclass->input['duration']);
			$this->ipsclass->input['duration'] = max($this->ipsclass->input['duration'],$this->settings['min_list_day'] * 86400);
			$this->ipsclass->input['duration'] = min($this->ipsclass->input['duration'],$this->settings['max_list_day'] * 86400);
			$this->ipsclass->input['duration'] = $this->ipsclass->input['duration'] - $this->ipsclass->input['duration'] % 86400;
			$this->ipsclass->input['user_description'] = $this->ipsclass->han_editor->process_raw_post( 'user_description' );
			
			$time = time();
			
			if($item['expired'] && $this->ipsclass->input['start_bid'] + $this->ipsclass->input['duration'] > $item['expired'])
			{
				$this->ipsclass->Error(array('MSG'=>'auction_no_end_after_expire'));
			}
			
			$new_item = array(
								'member_id' => $this->ipsclass->member['id'],
								'item' => $item['item'],
								'price_bought' => $item['price_bought'],
								'resell_price' => $item['resell_price'],
								'time_bought' => $item['time_bought'],
								'settings' => $item['settings'],
								'expired' => $item['expired'],
								'ip_address' => $this->ipsclass->ip_address,
								'time_start' => $time,
								'duration' => $this->ipsclass->input['duration'],
								'time_end' => $time + $this->ipsclass->input['duration'],
								'start_bid' => $this->ipsclass->input['start_bid'],
								'user_description' => $this->ipsclass->input['user_description'],
								'highest_bid' => $this->ipsclass->input['start_bid'],
								'highest_id' => 0,
							);
			
			$this->ipsclass->DB->do_insert('market_auction_item',$new_item);
			$item_id = $this->ipsclass->DB->get_insert_id();
			
			$this->ipsclass->DB->do_update('market_auction_item',array(
																		'removed' => 2,
										),"a_id=".$item['a_id']		);
			
			$this->ipsclass->market_class->redirect('auction_item_relisted',"plugin=".$this->plugin['link']."&do=item&item=".$item_id);
		}
		else
		{
			$this->ipsclass->han_editor->remove_side_panel = 1;
	        
			$this->ipsclass->subtitle = $this->ipsclass->lang['auction_list_items'];
			$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->plugin['name']."</a>","<a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['auction_list_items']."</a>");
			
			if ( $this->ipsclass->han_editor->method == 'rte' )
			{
				$item['user_description'] = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $item['user_description'] ) );
				$item['user_description'] = $this->ipsclass->parser->convert_ipb_html_to_html( $item['user_description'] );
			}
			else
			{
				$item['user_description'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $item['user_description'] ) );
			}
			
			
			$item['html_duration'] = "";
			for($i = intval($this->settings['min_list_day']); $i <= $this->settings['max_list_day']; $i++)
			{
				if($i == 1)
				{
					$item['html_duration'] .= "\t\t\t\t\t<option value='". 86400 ."'".($item['duration'] == 86400 ? " selected='selected'" : "").">".$this->ipsclass->lang['one_day']."</option>\n";
				}
				else
				{
					$item['html_duration'] .= "\t\t\t\t\t<option value='".($i*86400)."'".($item['duration'] == $i*86400 ? " selected='selected'" : "").">".sprintf($this->ipsclass->lang['x_days'],$i)."</option>\n";
				}
			}
			
			$item['expired'] = $item['expired'] == 0 ? $this->ipsclass->lang['never'] : $this->ipsclass->get_date($item['expired'],'TINY');
			$item['time_bought'] = $this->ipsclass->get_date($item['time_bought'],'TINY');
			$item['fprice_bought'] = $item['price_bought'] <= 0 ? $this->ipsclass->lang['free'] : $this->ipsclass->numbers->format_currency($item['price_bought']);
			$item['fresell_price'] = $item['resell_price'] <= 0 ? $this->ipsclass->lang['trash'] : $this->ipsclass->numbers->format_currency($item['resell_price']);
			$item['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item)));
			
			$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->auction_relist_form($item,$this->ipsclass->han_editor->show_editor( $item['user_description'], 'user_description' ));
		}
	}
	
	function run_remove_item()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_invalid_item'));
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
			$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']);
		}
		if($item['member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'auction_not_your_item'));
		}
		if($item['time_end'] > time())
		{
			$this->ipsclass->Error(array('MSG'=>'auction_not_ended'));
		}
		
		$this->ipsclass->DB->do_insert('market_inventory',array(
																	'member_id' => $this->ipsclass->member['id'],
																	'item' => $item['item'],
																	'price_bought' => $item['price_bought'],
																	'resell_price' => $item['resell_price'],
																	'time_bought' => $item['time_bought'],
																	'settings' => $item['settings'],
																	'expired' => $item['expired'],
										)						);
		
		$this->ipsclass->DB->do_update('market_auction_item',array(
																	'removed' => 1,
									),"a_id=".$item['a_id']		);
		
		$this->ipsclass->market_class->redirect('auction_item_removed',"CODE=inventory");
	}
	
	function run_pay_for_item()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		$item = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_auction_item','where'=>"a_id=".$this->ipsclass->input['item']));
		if(!$item)
		{
			$this->ipsclass->Error(array('MSG'=>'auction_invalid_item'));
		}
		if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
		{
			$this->ipsclass->DB->do_delete('market_auction_item',"a_id=".$item['a_id']);
			$this->ipsclass->DB->do_delete('market_auction_bids',"aitem_id=".$item['a_id']);
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."plugin=".$this->plugin['link']);
		}
		if($item['time_end'] > time())
		{
			$this->ipsclass->Error(array('MSG'=>'auction_not_ended'));
		}
		if($item['highest_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'auction_not_your_item'));
		}
		
		if($this->ipsclass->numbers->compare($item['highest_bid'],$this->ipsclass->member[$this->ipsclass->vars['market_members_field']]) > 0)
		{
			$this->ipsclass->Error(array('MSG'=>'not_enough_money'));
		}
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$item['member_id']));
		
		$this->ipsclass->DB->do_insert('market_inventory',array(
																	'member_id' => $item['highest_id'],
																	'item' => $item['item'],
																	'price_bought' => $item['highest_bid'],
																	'resell_price' => $this->ipsclass->numbers->multiply($item['highest_bid'],$this->settings['percent_resell'],.01),
																	'time_bought' => $item['time_end'],
																	'settings' => $item['settings'],
																	'expired' => $item['expired'],
										)						);
		
		$this->ipsclass->DB->do_update('market_auction_item',array(
																	'paid' => 1,
																	'removed' => 1,
									),"a_id=".$item['a_id']		);
		$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$item['highest_bid']),
				),"id=".$this->ipsclass->member['id']);
		$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($member[$this->ipsclass->vars['market_members_field']],$item['highest_bid']),
							),"id=".$item['member_id']);
		
		$this->ipsclass->market_class->redirect('auction_item_paid',"CODE=inventory");
	}
	
	function order($a,$b)
	{
		if($this->order[$this->ipsclass->input['order']] != '')
		{
			if($a[$this->order[$this->ipsclass->input['order']]] > $b[$this->order[$this->ipsclass->input['order']]])
			{
				return 1 * $this->order_by[$this->ipsclass->input['by']];
			}
			elseif($a[$this->order[$this->ipsclass->input['order']]] < $b[$this->order[$this->ipsclass->input['order']]])
			{
				return -1 * $this->order_by[$this->ipsclass->input['by']];
			}
			else
			{
				return 0;
			}
		}
		else
		{
			$translate = array('name'=>'item_name','icon'=>'icon');
			if($this->ipsclass->input['order'] == 'category')
			{
				$a_2 = $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$a['item']]['category']];
				$b_2 = $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$b['item']]['category']];
				if($a_2 < $b_2)
				{
					return 1 * $this->order_by[$this->ipsclass->input['by']];
				}
				elseif($a_2 > $b_2)
				{
					return -1 * $this->order_by[$this->ipsclass->input['by']];
				}
				else
				{
					return 0;
				}
			}
			else
			{
				$a_2 = $this->ipsclass->cache['market_items'][$a['item']][$translate[$this->ipsclass->input['order']]];
				$b_2 = $this->ipsclass->cache['market_items'][$b['item']][$translate[$this->ipsclass->input['order']]];
				return strcasecmp($a_2,$b_2) * $this->order_by[$this->ipsclass->input['by']];
			}
		}
	}
	
	function min_increment($highest_bid=0)
	{
		if($this->ipsclass->numbers->compare($highest_bid,250) < 0)
		{
			return 1;
		}
		
		$factor = 1;
		$t = $highest_bid;
		while($this->ipsclass->numbers->compare($t,1000) >= 0)
		{
			$factor = $this->ipsclass->numbers->multiply($factor,10);
			$t = $this->ipsclass->numbers->divide($t,10);
		}
		
		if($this->ipsclass->numbers->compare($t,250) < 0)
		{
			return $this->ipsclass->numbers->divide($this->ipsclass->numbers->multiply($factor,25),10);
		}
		elseif($this->ipsclass->numbers->compare($t,500) < 0)
		{
			return $this->ipsclass->numbers->multiply($factor,5);
		}
		else
		{
			return $this->ipsclass->numbers->multiply($factor,10);
		}
	}
}
?>